package com.bifrii.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.bifrii.transferObjects.Vote;

public class JDBCVoteDAO implements IVoteDAO{

	@Override
	public void insert(Integer idEvent, Vote v) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		
		String sql = "INSERT INTO Vote (oid, idEvent, idUser, vote) VALUES (?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, v.getOid());
            stmt.setInt(2, idEvent);
            stmt.setInt(3, v.getIdUser());
            stmt.setBoolean(4, v.getVote());

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}

	@Override
	public Vote select(Integer idUser, Integer idEvent) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        Vote v = null;
        String sql = "SELECT * FROM Vote WHERE (idUser = ?) and (idEvent = ?)";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idUser);
            stmt.setInt(2, idEvent);
            result = stmt.executeQuery();

            result.next();
            v = new Vote();
            v.setIdUser(result.getInt("idUser"));
            v.setIdEvent(result.getInt("idEvent"));
            v.setVote(result.getBoolean("vote"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return v;
	}

	@Override
	public List<Vote> selectAll(Integer idEvent) {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Vote> searchResults = new LinkedList<Vote>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Vote WHERE (idEvent = ?)";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idEvent);
            stmt.executeQuery();
            
            result = stmt.executeQuery();

            while (result.next()) {
                Vote temp = new Vote();
                temp.setOid(result.getString("oid"));
                temp.setIdEvent(result.getInt("idEvent"));
                temp.setIdUser(result.getInt("idUser"));
                temp.setVote(result.getBoolean("vote"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

}
